package com.ht.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.ht.bean.User;
import com.ht.common.Pager;

public class UserDaoImpl implements UserDao {

    private SessionFactory sessionFactory;
    private Session session;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    @Override
    public User add(User t) {
        session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        session.save(t);
        transaction.commit();
        session.close();
        return t;
    }

    @Override
    public User query(User t) {
        System.out.println("==========");
        session = sessionFactory.openSession();
        Query query = session.createQuery("from User t where t.uname=? and t.pwd = ?");
        query.setString(0, t.getUname());
        query.setString(1, t.getPwd());
        t = (User) query.uniqueResult();
        session.close();
        return t;
    }

    @Override
    public User update(User t) {
        session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        session.saveOrUpdate(t);
        transaction.commit();
        session.close();
        return t;
    }

    @Override
    public void delete(User t) {
        session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        session.delete(t);
        transaction.commit();
        session.close();
    }

    @Override
    public Pager<User> queryAll(Pager<User> pager) {
        session = sessionFactory.openSession();
        Query query = session.createQuery("from User");
        query.setFirstResult(pager.getBeginIndex());
        query.setMaxResults(pager.getPageSize());
        @SuppressWarnings("unchecked")
        List<User> list = query.list();
        pager.setRows(list);
        pager.setTotal((long) count());
        session.close();
        return pager;
    }

    @Override
    public Object count() {
        session = sessionFactory.openSession();
        Query q = session.createQuery("select count(t) from User t");
        Object obj = q.uniqueResult();
        return obj;

    }

}
